<template>
	<uv-input v-if="field.csmwjinput.format=='text' && field.csmwjinput.inputtype=='input'"
		:placeholder="field.csmwjinput.placeholder" border="surround" v-model="field.__answer__.value"
		@change="control_change"></uv-input>

	<uv-textarea v-if="field.csmwjinput.format=='text' && field.csmwjinput.inputtype=='textarea'"
		:placeholder="field.csmwjinput.placeholder" v-model="field.__answer__.value"
		@change="control_change"></uv-textarea>

	<uv-input v-if="field.csmwjinput.format=='number'" :placeholder="field.csmwjinput.placeholder" border="surround"
		v-model="field.__answer__.value" @change="control_change"></uv-input>

	<uv-input v-if="field.csmwjinput.format=='email'" :placeholder="field.csmwjinput.placeholder" border="surround"
		v-model="field.__answer__.value" @change="control_change" prefixIcon="email"></uv-input>

	<uv-input v-if="field.csmwjinput.format=='mobile'" :placeholder="field.csmwjinput.placeholder" border="surround"
		v-model="field.__answer__.value" @change="control_change" prefixIcon="phone"></uv-input>

	<uv-input v-if="field.csmwjinput.format=='realname'" :placeholder="field.csmwjinput.placeholder" border="surround"
		v-model="field.__answer__.value" @change="control_change" prefixIcon="account"></uv-input>

	<uv-input v-if="field.csmwjinput.format=='idcard'" :placeholder="field.csmwjinput.placeholder" border="surround"
		v-model="field.__answer__.value" @change="control_change" prefixIcon="order"></uv-input>

	<uv-input v-if="field.csmwjinput.format=='regex'" :placeholder="field.csmwjinput.placeholder" border="surround"
		v-model="field.__answer__.value" @change="control_change"></uv-input>


</template>

<script>
	import config from "@/config/config.js";
	import xcHttpUtils from '@/library/xcore/js/XcHttpUtils.js';
	import xcViewUtils from '@/library/xcore/js/XcViewUtils.js';
	import {
		xsStringUtils,
		xsDateUtils,
		xsArrayUtils
	} from 'xstack-for-javascript';

	export default {
		props: {
			field: {
				type: Object,
				default: []
			},
			paper: {
				type: Object,
				default: []
			},
		},
		watch: {
			'data': {
				handler(newVal, oldVal) {
					let that = this;
				},
			},
		},
		emits: ['change'],
		data() {
			return {
				config: config,
				xsStringUtils: xsStringUtils,
				xsDateUtils: xsDateUtils,
				sp: {},
				sr: {},
				control: {}
			}
		},
		created() {
			let that = this;
			that.xinit();
			that.public_show();
		},
		methods: {
			xinit() {
				let that = this;
			},
			public_show() {
				let that = this;
				if (that.field.__answer__.value == null) {
					that.field.__answer__.value = that.field.csmwjinput.defaultvalue;
				}
			},
			public_reset() {
				let that = this;
				that.public_show();
			},
			public_validate() {
				let that = this;
				let errmsg = null;
				let value = that.field.__answer__.value ? that.field.__answer__.value : "";

				if (that.field.csmwjrequired && value == "") {
					errmsg = that.$t("xc.rules_required_input");
					return errmsg;
				}

				switch (that.field.csmwjinput.format) {
					case "text":
						let ll = value.length;
						if (ll < that.field.csmwjinput.format_text_minlength || ll > that.field.csmwjinput
							.format_text_maxlength) {
							errmsg = that.$t('xp.paper.validate_string_length_range', {
								min: that.field.csmwjinput.format_text_minlength,
								max: that.field.csmwjinput.format_text_maxlength
							});
						}
						return errmsg;
					case "number":
						if (that.field.csmwjinput.format_number_isint === true) {
							if (parseInt(value) != value) {
								errmsg = that.$t('xp.paper.validate_number_isnotint');
								return errmsg;
							}
						}

						let val2 = parseFloat(value);
						if (value == val2) {
							if (val2 < that.field.csmwjinput.format_number_min || val2 > that.field.csmwjinput
								.format_number_max) {
								errmsg = that.$t('xp.paper.validate_int_range', {
									min: that.field.csmwjinput.format_number_min,
									max: that.field.csmwjinput.format_number_max
								});
							}
						} else {
							errmsg = that.$t('xp.paper.validate_number_isnotnumber');
						}
						return errmsg;
					case "email":
						let reg_email = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
						if (!reg_email.test(value)) {
							errmsg = that.$t('xp.paper.validate_email');
						}
						return errmsg;
					case "mobile":
						let reg_mobile =
							/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
						if (!reg_mobile.test(value)) {
							errmsg = that.$t('xp.paper.validate_mobile');
						}
						return errmsg;
					case "idcard":
						let reg_idcard =
							/^[1-9]\d{5}(18|19|20)?\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}([0-9Xx])$/;
						if (!reg_idcard.test(value)) {
							errmsg = that.$t('xp.paper.validate_idcard');
						}
						return errmsg;
					case "regex":
						let reg2 = new RegExp(that.field.csmwjinput.format_regex);;
						if (!reg2.test(value)) {
							errmsg = that.field.csmwjinput.format_regex_errormsg;
						}
						return errmsg;
				}
				return errmsg;
			},
			control_change() {
				let that = this;
				that.$emit("change");
				that.field.errmsg = that.public_validate();
			},
			_checkIDCard: function(idcode) {
				// 加权因子
				var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
				// 校验码
				var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];

				var code = idcode + "";
				var last = idcode[17]; //最后一位

				var seventeen = code.substring(0, 17);

				// ISO 7064:1983.MOD 11-2
				// 判断最后一位校验码是否正确
				var arr = seventeen.split("");
				var len = arr.length;
				var num = 0;
				for (var i = 0; i < len; i++) {
					num = num + arr[i] * weight_factor[i];
				}

				// 获取余数
				var resisue = num % 11;
				var last_no = check_code[resisue];

				// 格式的正则
				// 正则思路
				/*
				第一位不可能是0
				第二位到第六位可以是0-9
				第七位到第十位是年份，所以七八位为19或者20
				十一位和十二位是月份，这两位是01-12之间的数值
				十三位和十四位是日期，是从01-31之间的数值
				十五，十六，十七都是数字0-9
				十八位可能是数字0-9，也可能是X
				*/
				var idcard_patter =
					/^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;

				// 判断格式是否正确
				var format = idcard_patter.test(idcode);

				// 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
				return last === last_no && format ? true : false;
			},
		}
	}
</script>

<style lang="scss">

</style>